package lex.dm.id3;

import java.util.Enumeration;

import javax.swing.tree.TreeNode;

import org.lex.utils.collections.ArrayIterator;


public class ID3Node implements TreeNode
{
	public static final Object NULL = new Object();
	/**
	 * ���ڵ�
	 */
	ID3Node parent;
	/**
	 * ָ�򸸵��ĸ�����
	 */
	Object parentArrtibuteValue;
	/**
	 * ֻ�ڷ�Ҷ�ӽڵ��������壺������
	 */
	String attributeName;
	/**
	 * ֻ��Ҷ�ӽڵ��������壺�ж��սᣬԤ��ֵ
	 */
	Object leafValue=NULL;
	/**
	 * �ӽڵ�
	 */
	ID3Node[] children;

	// ---------------------------
	@SuppressWarnings( "unchecked" )
	public Enumeration children()
	{
		return new ArrayIterator<ID3Node>( children );
	}

	public boolean getAllowsChildren()
	{
		return children.length != 0;
	}

	public ID3Node getChildAt( int index )
	{
		return children[index];
	}

	public int getChildCount()
	{
		return children.length;
	}

	public int getIndex( javax.swing.tree.TreeNode node )
	{
		return org.lex.utils.Arrays.firstReference( children, node );
	}

	public ID3Node getParent()
	{
		return parent;
	}

	public boolean isLeaf()
	{
		return children.length == 0;
	}

	@Override
	public String toString()
	{
		if( this.isLeaf() )
		{
			return parent.attributeName + " == " + parentArrtibuteValue + " --> " + leafValue;
		}
		else
		{
			if( null == this.parentArrtibuteValue )
				//root
				return this.attributeName + "?";
			else
				return parent.attributeName + "==" + parentArrtibuteValue + ";  " + attributeName
						+ "?";
		}
	}
}
